<?php
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', 1);

date_default_timezone_set('Europe/Paris');
define('APPLICATION_PATH', realpath(dirname(__FILE__)));

set_include_path(implode(PATH_SEPARATOR, array(
	realpath(APPLICATION_PATH . '/lib'),
	realpath(APPLICATION_PATH . '/../static'),
	'.'
))); 

if (!defined('APPLICATION_ENV')) {
	define('APPLICATION_ENV', getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production');
}

// chargement des classes
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);

// Chargement de la configuration
$config = new Zend_Config_Ini('./conf/config.ini', 'prod');
$rights = new Zend_Config_Ini('./conf/rights.ini', 'all');
$registry = Zend_Registry::getInstance();
$registry->set('config', $config);
$registry->set('rights', $rights);


// Mise en place de la BDD
$pdoParams = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8;');

$db = Zend_Db::factory($config->db);
Zend_Db_Table::setDefaultAdapter($db);
Zend_Registry::set('dbAdapter', $db);

// setup controller
$frontController = Zend_Controller_Front::getInstance();
$frontController->throwExceptions(true);
$frontController->setControllerDirectory('./application/controllers');
Zend_Layout::startMvc(array('layoutPath'=>'./application/layouts'));

// Set routes
//$restRoute = new Zend_Rest_Route($frontController);
//$frontController->getRouter()->addRoute('default', $restRoute);
$restRoute = new Zend_Rest_Route($frontController, array(), array('default' => array('gugus', 'coloc')));
$frontController->getRouter()->addRoute('rest', $restRoute);



// initialize rights for unconnected users
$acl = new Zend_Acl();

foreach ($rights->rights as $role=>$resources) {
	$acl->addRole(new Zend_Acl_Role($role));
	foreach ($resources as $resource) {
		if (!$acl->has($resource)) {
			$acl->addResource(new Zend_Acl_Resource($resource));
		}
		$acl->allow($role, $resource);
	}
}

Zend_Registry::set('acl', $acl);


// run
$frontController->dispatch();
